package com.github.linkoog.model_designer.ide.editer.dbmodel;

import com.github.linkoog.model_designer.ide.editer.dbmodel.right_editer.DBModelRightPanel;
import com.github.linkoog.model_designer.ide.type.ModelYamlFileType;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.openapi.vfs.newvfs.BulkFileListener;
import com.intellij.openapi.vfs.newvfs.events.VFileEvent;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.ui.JBDimension;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class DBModelRightPanelInjector implements FileEditorManagerListener, BulkFileListener {

    private Project project;


    public DBModelRightPanelInjector(Project project){
        this.project = project;
        MessageBusConnection messageBusConnection = project.getMessageBus().connect();
        messageBusConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
        messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, this);
    }

    /**
     *   文件打开后，注入右侧可视化面板
     */
    @Override
    public void fileOpened(@NotNull FileEditorManager fileEditorManager, @NotNull VirtualFile virtualFile) {
        if (!(virtualFile.getFileType() instanceof ModelYamlFileType)){
            return;
        }


        FileEditor[] fileEditors = fileEditorManager.getEditors(virtualFile);
        for (FileEditor fileEditor : fileEditors) {
            if (!(fileEditor instanceof TextEditor)){
                continue;
            }

            inject(fileEditor);
        }
    }

    /**
     *   文件编辑后，保存时更新右侧可视化面板
     */
    @Override
    public void after(@NotNull List<? extends @NotNull VFileEvent> events) {
        for (VFileEvent event : events) {
            final VirtualFile virtualFile = event.getFile();
            if (!event.isFromSave() || virtualFile == null ||  !(virtualFile.getFileType() instanceof ModelYamlFileType)){
                continue;
            }

            final DBModelRightPanel dbTableLeftPanel = virtualFile.getUserData(DBModelConstants.DB_MODEL_RIGHT_PANEL_KEY);
            if (dbTableLeftPanel == null){
                continue;
            }

            dbTableLeftPanel.resetPanelContext();
            break;
        }
    }

    private void inject(FileEditor fileEditor) {
        JComponent editerComponent = fileEditor.getComponent();
        Container editerParentComponent = editerComponent.getParent();
        if (!(editerParentComponent instanceof JPanel)){
            return;
        }

        JPanel containerPanel = (JPanel)editerParentComponent;
        containerPanel.setMinimumSize(new JBDimension(80, -1));

        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.72F);
        splitter.setResizeEnabled(true);
        splitter.setDividerPositionStrategy(Splitter.DividerPositionStrategy.KEEP_SECOND_SIZE);
        splitter.setFirstComponent(editerComponent);

        DBModelRightPanel leftPanel = new DBModelRightPanel(project, fileEditor, fileEditor.getFile());
        splitter.setSecondComponent(leftPanel.getMyMainPanel());

        containerPanel.add(splitter, BorderLayout.CENTER);
    }



}
